import React, {Component} from 'react';
import './SchemeType.scss'

import ChoosePharmavy from './ChoosePharmavy'

import List from '@material-ui/core/List';
import ListItem from '@material-ui/core/ListItem';
import ListItemText from '@material-ui/core/ListItemText';
import Divider from '@material-ui/core/Divider';
import Icon from '@material-ui/core/Icon';
import Grid from '@material-ui/core/Grid';
import Button from '@material-ui/core/Button';
import SwipeableDrawer from '@material-ui/core/SwipeableDrawer';
import AppBar from '@material-ui/core/AppBar';
import Toolbar from '@material-ui/core/Toolbar';
import Typography from '@material-ui/core/Typography';
import CircularProgress from '@material-ui/core/CircularProgress';


class SchemeType extends Component {
    state={
        typeData: [],
        typeIndexActive:0,
        bottomDrawer:false,
        user:{},
        typeEd:{},
        supplier:{},
        isHasSup:false,
        isHasSupShow:true,
        faultSupplier:{},
        supplier_select_service:0,
        supplier_select_servicee:0,
    }
    gainTypes = () =>{
        // return new Promise((resolve, reject)=>{
            fetch('http://w.71cp.com/doctor_ajax.php?do=gainTypes', {
                method: 'POST',
            }).then((response) => {
                response.json().then((data) => {
                    if ( data.code === 1 ){
                        this.setState({
                            typeData:data.data,
                            typeEd:data.data[0],
                        })
                        // this.getSupByTYPE(data.data[0])
                    }else{
                        console.log(data.msg)
                    }
                })
            }).catch((error) => {
                alert(error)
            })
        // })



    }

    historystore = () => {
        let param = new FormData();
        param.append('uid', 1447329);
        fetch('http://w.71cp.com/doctor_ajax.php?do=getUserOrderInfo', {
            method: 'POST',
            body: param
        }).then((response) => {
            response.json().then((data) => {
                console.log(data)
            })
        }).catch((error) => {
            alert(error)
        })
        this.setState({
            bottomDrawer:true
        })
    }
    chooseType = (item,index) => {
        this.setState({
            typeEd:item,
            typeIndexActive:index,
        })
    }

    toggleDrawer = (bool) => {
        this.setState({
            bottomDrawer:bool
        })
    }
    getDataToS = (val) => {
        if(val!==this.state.isHasSup){
            this.setState({
                isHasSup:val
            })
        }
    }
    getDataTodefaultSupplierS = (val) => {
        if(val.fw_id !== this.state.faultSupplier.fw_id){
            this.setState({
                faultSupplier:val
            })
        }
    }
    getDataToDefaultTypeS = (val) =>{
        if(val.supplier_select_service !== this.state.supplier_select_service.supplier_select_service){
            this.setState({
                supplier_select_service:val
            })
        }
    }
    getDataToChooseTypeS = (val) => {
        if(val.supplier_select_service !== this.state.supplier_select_servicee.supplier_select_service){
            this.setState({
                supplier_select_servicee:val
            })
        }
    } 
    nextStep = () => {
        console.log("下一步")
        let x = 0;
        if (this.state.supplier_select_servicee.supplier_select_service) {
            x = this.state.supplier_select_servicee.supplier_select_service
        } else {
            x = this.state.supplier_select_service.supplier_select_service
        }
        let param = new FormData();
        param.append('type', this.state.typeEd.type);
        param.append('supplier_id_default', this.state.faultSupplier.supplier_id);
        param.append('supplier_select_service', x);
        fetch('http://w.71cp.com/doctor_ajax.php?do=beforePrescribe', {
            method: 'POST',
            body: param
        }).then((response) => {
            response.json().then((data) => {
                console.log(data)
                if(data.msg==="SUCCESS"){
                    this.props.history.push('/Proposal');
                }
            })
        }).catch((error) => {
            alert(error)
        })
    }
    componentWillMount() {
        this.gainTypes();
    }
    render() {
        
        const typeData=this.state.typeData.map((item,index)=>{
            return (
                <Grid item xs={6} key={item.type}>
                    <Button variant="contained"  color={this.state.typeIndexActive===index?'secondary':'inherit'} className="typePaper"  onClick={this.chooseType.bind(this,item,index)}>
                        {item.name}
                    </Button>
                </Grid>
            )
        })
        
        return (
            <div className="SchemeType" >
                <List className="historystore" onClick={this.historystore.bind(this)}>
                    <ListItem>
                        <ListItemText primary="调用历史方案" secondary="您已为他开过建议，可在此基础上调整" />
                        <Icon>keyboard_arrow_right</Icon>
                    </ListItem>
                </List>
                <SwipeableDrawer 
                    anchor="bottom"
                    open={this.state.bottomDrawer}
                    onClose={this.toggleDrawer.bind(this, false)}
                    onOpen={this.toggleDrawer.bind(this, true)}>
                    <div 
                        tabIndex={0}
                        role="button"
                        onClick={this.toggleDrawer.bind(this, false)}
                        onKeyDown={this.toggleDrawer.bind(this, false)}>
                        <AppBar position="static">
                            <Toolbar>
                                <Typography variant="title" color="inherit" >
                                    历史方案
                                </Typography>
                            </Toolbar>
                        </AppBar>
                        <CircularProgress  color="secondary" className="CircularProgressed"/>
                    </div>
                </SwipeableDrawer>
                <Divider />

                <Grid container spacing={0}>
                    {typeData}
                </Grid>
                
                {
                    this.state.isHasSupShow?(
                        <ChoosePharmavy 
                        typeed={this.state.supplier} 
                        type={this.state.typeEd}  
                        getDataToisHasSup={this.getDataToS.bind(this)}
                        getDataTodefaultSupplier={this.getDataTodefaultSupplierS.bind(this)}
                        getDataToDefaultType={this.getDataToDefaultTypeS.bind(this)}
                        getDataToChooseType={this.getDataToChooseTypeS.bind(this)}
                        />
                    ):null
                }

                 <Button variant="contained" color='secondary'  disabled={this.state.isHasSup}  className="nextStep" onClick={this.nextStep.bind(this)}>
                    下一步
                </Button>
            </div>
        );
    }
}

export default SchemeType;
